package com.aoxin.service.impl.course.systemCourse;

import java.util.List;

import com.aoxin.model.course.SchoolCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.systemCourse.SchoolCourseDao;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfEmployee;
import com.aoxin.service.inter.course.systemCourse.SchoolCourseService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("SlfSchoolCourseService")
public class SchoolCourseServiceImp implements SchoolCourseService {
    @Autowired
    private SchoolCourseDao schoolCourseDao;

    /**
     * 查询某个学校课程
     *
     * @param schoolCourse
     * @return
     */
    @Override
    public List<SchoolCourse> getSchoolCourse(SchoolCourse schoolCourse) {
        String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
        schoolCourse.setSchoolId(schoolId);
        Assert.notNull(schoolCourse.getSchoolId(), String.format(ErrorUtil.SCHOOL_COURSE_SYSTEM, "schoolId"));
        List<SchoolCourse> list = schoolCourseDao.getSchoolCourse(schoolCourse);
        return list;
    }

    /**
     * 新增某个学校课程
     * @param courseIds
     * @return
     */
    @Override
    public Integer insertSchoolCourse(Integer courseIds[]) throws Exception {
        Integer num = 0;
        Integer checkNum = 0;
        Assert.notEmpty(courseIds, ErrorUtil.SYSTEM_COURSE_ARRAY);
        SlfEmployee employee = ThreadLocalUtil.getEmployee();
        String shcoolId = String.valueOf(employee.getSchoolId());
        String employeeName=employee.getName();
        Integer employeeId= employee.getEmployeeId();
        System.err.println("id:" + shcoolId);
        for (Integer courseId : courseIds) {
            Integer check = schoolCourseDao.checkSchoolCourse(shcoolId, courseId);
            System.out.println("check:"+check);
            if (check == 0) {
                checkNum++;
            } else {
                throw new Exception(ErrorUtil.SYSTEM_COURSE_SCHOOL);
            }
        }
        if (checkNum == courseIds.length) {
            for (Integer courseId : courseIds) {
                SchoolCourse course = schoolCourseDao.getCourse(courseId);
                course.setSchoolId(shcoolId);
                course.setEmployeeName(employeeName);
                course.setEmployeeId(employeeId);
                if(course.getName()!=null&&course.getCourseType()!=null){
                    System.out.println("get course success!");
                    Integer col = schoolCourseDao.insertSchoolCourse(course);
                    if (col > 0) {
                        System.out.println("新增成功");
                        num++;
                    } else {
                        throw new Exception(ErrorUtil.SYSTEM_COURSE_ARRAY);
                    }
                }else{
                    throw  new Exception(ErrorUtil.SYSTEM_COURSE_ARRAY);
                }

            }
        } else {
            throw new Exception(ErrorUtil.SYSTEM_COURSE_SCHOOL);
        }
        if (num == courseIds.length&&checkNum==courseIds.length) {
            return num;
        } else {
            throw new Exception(ErrorUtil.SYSTEM_COURSE_ARRAY);
        }

    }

    /**
     * 修改某个学校课程
     *
     * @param schoolCourse
     * @return
     */
    @Override
    public Integer updateSchoolCourse(SchoolCourse schoolCourse) throws Exception {
        System.out.println("entity:"+ schoolCourse.getSchoolCourseId());
        SlfEmployee employee = ThreadLocalUtil.getEmployee();
        String schoolId=String.valueOf(employee.getSchoolId());
        schoolCourse.setSchoolId(schoolId);
        Assert.notNull(schoolId,ErrorUtil.SCHOOL_COURSE_SYSTEM);
        //查课程库中的课程数据
        SchoolCourse entity = schoolCourseDao.getAloneCourse(schoolCourse.getCourseId());

        if(entity.getCourseId()!=null&&entity.getCourseType()!=null&&entity.getName()!=null){
            entity.setSchoolId(schoolId);
            entity.setSchoolCourseId(schoolCourse.getSchoolCourseId());
            Integer integer = schoolCourseDao.checkSchoolCourseinsert(schoolId, entity.getSchoolCourseId(),entity.getName());
            if(integer==0){
                System.out.println("entity:"+entity.toString());
                return schoolCourseDao.updateSchoolCourse(entity);
            }else{
                throw  new Exception(ErrorUtil.COURSE_CHECK);
            }
        }else{
            throw new Exception(ErrorUtil.SCHOOL_UPDATE_COURSE);
        }
    }


    @Override
    public List<SchoolCourse> displaySchoolCourse(SchoolCourse schoolCourse) {
        Assert.notNull(schoolCourse,String.format(ErrorUtil.SCHOOL_COURSE_SYSTEM,"schoolCourse"));
        List<SchoolCourse> list=schoolCourseDao.displaySchoolCourse(schoolCourse);
        return list;
    }

    @Override
    public List<SchoolCourse> getPageCourse(Integer schoolCoursePageId) {
        Assert.notNull(schoolCoursePageId,String.format(ErrorUtil.PARAMETER_NULL,"schoolCoursePageId"));
        return this.schoolCourseDao.getPageCourse(schoolCoursePageId);
    }

    @Override
	public List<SelectDto> getListBySchool() {
		return this.schoolCourseDao.getListBySchoolId(ThreadLocalUtil.getEmployee().getSchoolId());
	}

	@Override
	public List<SchoolCourse> getSchoolCourseUser(SchoolCourse schoolCourse) {
	        List<SchoolCourse> list = schoolCourseDao.getSchoolCourse(schoolCourse);
	        return list;
	}



}
